﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System;
using System.IO;
using Telerik.Web.Mvc.UI;

namespace Presentation.Web.Admin.Controllers
{
    [HandleError]
    public class ImageBrowserController : EditorFileBrowserController
    {
        Infrastructure.Crosscutting.Authorize.IUserService userService;

        public ImageBrowserController(Infrastructure.Crosscutting.Authorize.IUserService user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("userService is null");
            }
            userService = user;
        }

        private const string contentFolderRoot = "~/upload/article/";

        private static readonly string[] foldersToCopy = new[] { "~/upload/article/" };

        public override string[] ContentPaths
        {
            get
            {
                return new[] { CreateUserFolder() };
            }
        }

        protected string CreateUserFolder()
        {
            var virtualPath = Path.Combine(contentFolderRoot, this.SessionUser().Guid);

            var path = Server.MapPath(virtualPath);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                //foreach (var sourceFolder in foldersToCopy)
                //{
                //    CopyFolder(Server.MapPath(sourceFolder), path);
                //}
            }
            return virtualPath;
        }

        protected void CopyFolder(string source, string destination)
        {
            if (!Directory.Exists(destination))
            {
                Directory.CreateDirectory(destination);
            }
            foreach (var file in Directory.EnumerateFiles(source))
            {
                var dest = Path.Combine(destination, Path.GetFileName(file));
                System.IO.File.Copy(file, dest);
            }
            foreach (var folder in Directory.EnumerateDirectories(source))
            {
                var dest = Path.Combine(destination, Path.GetFileName(folder));
                CopyFolder(folder, dest);
            }
        }

        protected Domain.Entity.User SessionUser()
        {
            var user = Session[Infrastructure.Crosscutting.Comm.Service.ConstService.SessionNameCurrentUser]
                 as Domain.Entity.User;
            user = userService.GetDefautlOperUser();
            return user;
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var user = Session[Infrastructure.Crosscutting.Comm.Service.ConstService.SessionNameCurrentUser]
                as Domain.Entity.User;
            if (user == null)
            {
                filterContext.Result = new RedirectResult("/login/");
            }
            base.OnActionExecuting(filterContext);
        }
    }
}
